package com.example.config;

import com.example.service.SuppliesService;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static com.example.config.SocketHandler.*;

@Slf4j
@Data
@Component
//@PropertySource("classpath:socket.properties")
@NoArgsConstructor
public class SocketServer {


    @Autowired
    private SuppliesService suppliesService;
    @Value("${port}")
    private Integer port;
    private boolean started;
    private ServerSocket serverSocket;
    //每当有一个客户端连接进来后，就启动一个单独的线程进行处理
    //创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程。
    private ExecutorService executorService = Executors.newCachedThreadPool();
    //创建一个定长线程池，可控制线程最大并发数，超出的线程会在队列中等待。
//    private ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
    // 缓存一个read事件中一个不完整的包，以待下次read事件到来时拼接成完整的包
    ByteBuffer cacheBuffer = ByteBuffer.allocate(100);
    boolean cache = false;

    public static void main(String[] args){
        new SocketServer().start(8068);
    }

    public void start(){
        start(null);
    }

//    @Autowired
//    private SuppliesRealinfoService suppliesRealinfoService;//测试使用


    public void start(Integer port){
        log.info("port: {}, {}", this.port, port);
        try {
            serverSocket =  new ServerSocket(port == null ? this.port : port);
            started = true;
            log.info("Socket服务已启动，占用端口： {}", serverSocket.getLocalPort());
        } catch (IOException e) {
            log.error("端口冲突,异常信息：{}", e);
            System.exit(0);
        }
        //创建一个线程池
//        ExecutorService executorService = Executors.newFixedThreadPool(100);
        while (started){
            try {
                Thread.sleep(10);
                Socket socket = serverSocket.accept();
                //长连接
                socket.setKeepAlive(true);
                ClientSocket register = register(socket);
                log.info("客户端已连接，其Key值为：{}", register.getKey());
                    if (register != null) {
                        executorService.submit(register);
                    }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
